package com.gzz.orchid.common.boot.mybatis.dialect;

import com.gzz.orchid.common.core.model.Pagination;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.session.RowBounds;

/**
 * MySQL 方言
 *
 * @author higmd
 */
public class MySqlDialect implements IDialect {
    @Override
    public void afterAll() {
    }

    @Override
    public String rebuildSql(BoundSql boundSql) {
        return boundSql.getSql() + " LIMIT ?,?";
    }


    @Override
    public String rebuildSql(String sql, Pagination pager) {
        int pageSize = (pager.getTotal() - pager.getOffset()) > pager.getSize() ? pager.getSize() : (pager.getTotal() - pager.getOffset());
        return sql + " limit " + pager.getOffset() + "," + pageSize;
    }

    @Override
    public String rebuildSql(String sql, int offset, int pageSize) {
        return sql + " limit " + offset + "," + pageSize;
    }
}
